package cn.yuanqiao.archive.bianyan.service.Impl;

import cn.yuanqiao.archive.bianyan.domain.YqDaGerenKu;
import cn.yuanqiao.archive.bianyan.domain.YqDataZhuantiGern;
import cn.yuanqiao.archive.bianyan.mapper.YqDaGerenKuMapper;
import cn.yuanqiao.archive.bianyan.mapper.YqDataZhuantiGernMapper;
import cn.yuanqiao.archive.bianyan.service.IYqDaGerenKuService;
import cn.yuanqiao.archive.liyong.mapper.ListQueryMapper;
import cn.yuanqiao.archive.liyong.service.ListQueryService;
import cn.yuanqiao.common.constant.Constants;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 档案资源库Service业务层处理
 *
 * @ProjectName: JZSDAS
 * @Package: cn.yuanqiao.archive.bianyan.service.Impl
 * @ClassName: YqDaGerenKuServiceImpl
 * @Author: lenovo
 * @Description: hbWang
 * @Date: 2023/9/18 17:19
 * @Version: 1.0
 */
@Service
public class YqDaGerenKuServiceImpl implements IYqDaGerenKuService {
    @Autowired
    private YqDaGerenKuMapper yqDaGerenKuMapper;

    @Autowired
    private YqDataZhuantiGernMapper yqDataZhuantiGernMapper;

    @Autowired
    private ListQueryService listQueryService;

    @Autowired
    ListQueryMapper listQueryMapper;


    /**
     * 查询档案资源库
     *
     * @param id 档案资源库主键
     * @return 档案资源库
     */
    @Override
    public YqDaGerenKu selectYqDaGerenKuById(String id) {
        return yqDaGerenKuMapper.selectYqDaGerenKuById(id, new YqDaGerenKu().getTableName());
    }

    /**
     * 查询档案资源库列表
     *
     * @param yqDaGerenKu 档案资源库
     * @return 档案资源库
     */
    @Override
    public List<YqDaGerenKu> selectYqDaGerenKuList(YqDaGerenKu yqDaGerenKu) {
        yqDaGerenKu.setCreatorId(String.valueOf(SecurityUtils.getUserId()));
        return yqDaGerenKuMapper.selectYqDaGerenKuList(yqDaGerenKu, yqDaGerenKu.getTableName());
    }
//
//    /**
//     * 根据专题id查询资料
//     *
//     * @param id
//     * @param daTitle
//     * @return
//     */
//    @Override
//    public List<YqDaGerenKu> selectZTDataList(String id, String daTitle) {
//        String tableCode = SecurityUtils.getTenantCode();
//        String tableNameTp = tableCode + Constants.TABLE_YQ_ZHUANTI_GEREN_DATA;
//        return yqDaGerenKuMapper.selectZTDataList(daTitle, new YqDaGerenKu().getTableName(), id, tableNameTp);
//    }

    /**
     * 批量新增档案资源库
     *
     * @param yqDaGerenKus 档案资源库
     * @return 结果
     */
    @Override
    public int insertYqDaGerenKu(List<YqDaGerenKu> yqDaGerenKus) {
        String tenantCode = SecurityUtils.getTenantCode();
        String type = yqDaGerenKus.get(0).getDaType();
        String daIds = yqDaGerenKus.stream().map(m -> m.getDaId()).collect(Collectors.joining("','"));
        StringBuilder sql = new StringBuilder();
        List<YqDaGerenKu> yqDaGerenKusda = new ArrayList<>();
        if (type.equals("0")) {
            yqDaGerenKusda.addAll(yqDaGerenKus);
        }
        if (type.equals("1")) {
            sql.append("select da.DA_ID , da.SUB_TABLE_NAME,da.ND,da.MJ,da.BGQX,da.TITLE,da.DH  from " +
                            tenantCode + Constants.BIG_TABLE_FOLDER_NAME + " fd ")
                    .append(" left join " + tenantCode + Constants.BIG_TABLE_YQDA + " da on fd.DA_ID = da.DA_ID ")
                    .append(" where da.IS_DEL = 0 and da.STATE = '5' and fd.FOLDER_ID in ('")
                    .append(daIds).append("') ");
            yqDaGerenKusda = yqDaGerenKuMapper.selectDaList(sql.toString());
        }
        if (type.equals("3")) {
            sql.append("select da.DA_ID , da.SUB_TABLE_NAME,da.ND,da.MJ,da.BGQX,da.TITLE,da.DH  from " +
                            tenantCode + Constants.BIG_TABLE_NAME_ITEM + " fi ")
                    .append(" left join " + tenantCode + Constants.BIG_TABLE_FOLDER_NAME + " fd on fd.FOLDER_ID = fi.FOLDER_ID ")
                    .append(" left join " + tenantCode + Constants.BIG_TABLE_YQDA + " da on fd.DA_ID = da.DA_ID ")
                    .append(" where da.IS_DEL = 0 and da.STATE = '5' and fi.ITEM_ID in ('")
                    .append(daIds).append("') ");
            yqDaGerenKusda = yqDaGerenKuMapper.selectDaList(sql.toString());
        }
        for (int i = 0; i < yqDaGerenKusda.size(); i++) {
            YqDaGerenKu yqDaGerenKu = yqDaGerenKusda.get(i);
            YqDaGerenKu yqDaGerenKu1 = yqDaGerenKuMapper.selectDaId(yqDaGerenKu.getDaId(), yqDaGerenKu.getTableName());
            if (yqDaGerenKu1 != null) {
                continue;
            }
            yqDaGerenKu.setId(String.valueOf(SnowflakeIdGenerator.generateId()));
            yqDaGerenKu.setCreateTime(DateUtils.getNowDate());
            yqDaGerenKu.setTypeState("1");
            yqDaGerenKu.setDaType("0");
            yqDaGerenKu.setCreatorBy(SecurityUtils.getUsername());
            yqDaGerenKu.setCreatorId(String.valueOf(SecurityUtils.getUserId()));
            yqDaGerenKu.setCategoryId(yqDaGerenKus.get(0).getCategoryId());
            yqDaGerenKuMapper.insertYqDaGerenKu(yqDaGerenKu, yqDaGerenKu.getTableName());
        }
        return 1;
    }

    /**
     * 添加至专题库
     *
     * @param
     * @return
     */
    @Override
    @Transactional
    public int insertYqZhuanTiList(List<YqDataZhuantiGern> yqDataZhuantiGern) {
        //获取表名信息
        String tableCode = SecurityUtils.getTenantCode();
        String tableNameTp = tableCode + Constants.TABLE_YQ_ZHUANTI_GEREN_DATA;
        String gerenIds = yqDataZhuantiGern.stream().map(m -> m.getGerenId()).collect(Collectors.joining(","));
        List<YqDaGerenKu> yqDaGerenKus = yqDaGerenKuMapper.selectDaIdS(gerenIds.split(","), new YqDaGerenKu().getTableName());
        String geId = yqDaGerenKus.stream().map(m -> m.getId()).collect(Collectors.joining(","));
        //修改个人库状态
        yqDaGerenKuMapper.updateYqDaGerenKuState(geId.split(","), "2", new YqDaGerenKu().getTableName());
        List<YqDataZhuantiGern> yqDataList = new ArrayList<>();
        yqDaGerenKus.forEach(y -> {
            YqDataZhuantiGern yqData = new YqDataZhuantiGern();
            yqData.setGerenId(y.getId());
            yqData.setZhuantiId(yqDataZhuantiGern.get(0).getZhuantiId());
            yqData.setState("5");
            yqDataList.add(yqData);
        });
        return yqDataZhuantiGernMapper.insertYqDataZhuantiGern(yqDataList, tableNameTp);
    }


    /**
     * 修改档案资源库
     *
     * @param yqDaGerenKu 档案资源库
     * @return 结果
     */
    @Override
    public int updateYqDaGerenKu(YqDaGerenKu yqDaGerenKu) {
        return yqDaGerenKuMapper.updateYqDaGerenKu(yqDaGerenKu, yqDaGerenKu.getTableName());
    }

    /**
     * 退回至个人库
     *
     * @param gerenIds
     * @return
     */
    @Override
    @Transactional
    public int deleteYqZhuanTiByIDs(String[] gerenIds) {
        //获取表名信息SecurityUtils.getTenantCode()
        String tableCode = SecurityUtils.getTenantCode();
        String tableNameTp = tableCode + Constants.TABLE_YQ_ZHUANTI_GEREN_DATA;
        List<YqDaGerenKu> yqDaGerenKus = yqDaGerenKuMapper.selectDaIdS(gerenIds, new YqDaGerenKu().getTableName());
        String geId = yqDaGerenKus.stream().map(m -> m.getId()).collect(Collectors.joining(","));
        yqDataZhuantiGernMapper.deleteYqDataZhuantiGernByGerenIds(geId.split(","), tableNameTp);
        return yqDaGerenKuMapper.updateYqDaGerenKuState(geId.split(","), "1", new YqDaGerenKu().getTableName());
    }

    /**
     * 添加至公共库
     *
     * @param gerenIds
     * @return
     */
    @Override
    public int communalUpdate(String[] gerenIds, String typeState) {
        List<YqDaGerenKu> yqDaGerenKus = yqDaGerenKuMapper.selectDaIdS(gerenIds, new YqDaGerenKu().getTableName());
        String geId = yqDaGerenKus.stream().map(m -> m.getId()).collect(Collectors.joining(","));
        return yqDaGerenKuMapper.updateYqDaGerenKuState(geId.split(","), typeState, new YqDaGerenKu().getTableName());
    }


    /**
     * 移除公共库
     *
     * @param gerenIds
     * @return
     */
    @Override
    public int removeCommunal(String[] gerenIds, String typeState) {
        return yqDaGerenKuMapper.updateYqDaGerenKuremove(gerenIds, typeState, new YqDaGerenKu().getTableName());
    }


    /**
     * 批量删除档案资源库
     *
     * @param ids 需要删除的档案资源库主键
     * @return 结果
     */
    @Override
    public int deleteYqDaGerenKuByIds(String[] ids) {
        List<YqDaGerenKu> yqDaGerenKus = yqDaGerenKuMapper.selectDaIdS(ids, new YqDaGerenKu().getTableName());
        String geId = yqDaGerenKus.stream().map(m -> m.getId()).collect(Collectors.joining(","));
        return yqDaGerenKuMapper.deleteYqDaGerenKuByIds(geId.split(","), new YqDaGerenKu().getTableName());
    }

    /**
     * 删除档案资源库信息
     *
     * @param id 档案资源库主键
     * @return 结果
     */
    @Override
    public int deleteYqDaGerenKuById(String id) {
        return yqDaGerenKuMapper.deleteYqDaGerenKuById(id, new YqDaGerenKu().getTableName());
    }

}
